﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ConstructionBidOnline.Biz;
using ConstructionBidOnline.Common;

public partial class admin_ManageBids : System.Web.UI.Page
{
    #region Types

    public enum LayoutType
    {
        Add,
        Edit,
        Search,
        View
    }

    #endregion

    #region Constants

    private const string PROP_DATASRC_BIDS = "__BIDS::BIDS";
    private const string PROP_LAYOUT = "__BIDS::UIL";
    private const string PROP_SELECTED_BID_ID = "__BIDS::SBIDID";

    #endregion

    #region Public Properties

    public BidCollection DataSourceBids
    {
        get { return (SessionMgr.Session[PROP_DATASRC_BIDS] is BidCollection ? (BidCollection)SessionMgr.Session[PROP_DATASRC_BIDS] : (BidCollection)(SessionMgr.Session[PROP_DATASRC_BIDS] = new BidCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_BIDS] = value; }
    }

    public int SelectedBidID
    {
        get { return (SessionMgr.Session[PROP_SELECTED_BID_ID] is int ? (int)SessionMgr.Session[PROP_SELECTED_BID_ID] : -1); }
        set { SessionMgr.Session[PROP_SELECTED_BID_ID] = value; }
    }


    public LayoutType UILayout
    {
        get { return (SessionMgr.Session[PROP_LAYOUT] is LayoutType ? (LayoutType)SessionMgr.Session[PROP_LAYOUT] : LayoutType.Search); }
        set { SessionMgr.Session[PROP_LAYOUT] = value; }
    }


    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        if (!Page.IsPostBack)
        {
            DataSourceBids = Bid.GetBidByBidIsActive(false);
            NonActiveBidsList1.DataBind(DataSourceBids);
            DataSourceBids = Bid.GetBidByBidIsActive(true);
            ActiveBidsList1.DataBind(DataSourceBids);
        }
    }

    #endregion

    #region Control: NonActiveBidsList

    protected void ucNonActiveBidsList_EditRequested(object sender, Controls_NonActiveBidsList.EditRequestedEventArgs evt)
    {
        SelectedBidID = evt.B.BidID;
        BidsViewEdit1.DataBind(evt.B);
        SetLayout(LayoutType.Edit);
    }


    protected void ucNonActiveBidsList_AddNewRequested(object sender, EventArgs evt)
    {
        BidsViewEdit1.CancelEdit();
        Bid newBid = Bid.CreateNew(DataSourceBids);
        SelectedBidID = newBid.BidID;
        SetLayout(LayoutType.Edit);
    }


    protected void ucNonActiveBidsList_DataSourceRequested(object sender, EventArgs evt)
    {
        DataSourceBids = Bid.GetBidByBidIsActive(false);
        NonActiveBidsList1.DataSource = DataSourceBids;
    }

    #endregion

    #region Control: ActiveBidsList

    protected void ucActiveBidsList_EditRequested(object sender, Controls_ActiveBidsList.EditRequestedEventArgs evt)
    {
        SelectedBidID = evt.B.BidID;
        BidsViewEdit1.DataBind(evt.B);
        SetLayout(LayoutType.Edit);
    }


    protected void ucActiveBidsList_AddNewRequested(object sender, EventArgs evt)
    {
        BidsViewEdit1.CancelEdit();
        Bid newBid = Bid.CreateNew(DataSourceBids);
        SelectedBidID = newBid.BidID;
        //ConsultantViewEdit1.DataBind(DataSourceConsultants.FindByConsultantID(SelectedCONSULTANTID));
        SetLayout(LayoutType.Edit);
    }


    protected void ucActiveBidsList_DataSourceRequested(object sender, EventArgs evt)
    {
        DataSourceBids = Bid.GetBidByBidIsActive(true);
        ActiveBidsList1.DataSource = DataSourceBids;
    }

    #endregion

    #region Control: BidsViewEdit

    protected void ucBidsViewEdit_DataSourceBidRequested(object sender, EventArgs evt)
    {
        BidsViewEdit1.DataSourceBid = DataSourceBids.FindByBidID(SelectedBidID);
    }

    #endregion

    #region Save / Cancel Buttons

    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {
        BidsViewEdit1.CancelEdit();
        // If we were adding a new Bid...then remove the incomplete/not added new Bid object from the collection.
        if (DataSourceBids.FindByBidID(SelectedBidID).ObjState == BOBase.ObjectState.Added)
        {
            try
            {
                DataSourceBids.Remove(DataSourceBids.FindByBidID(SelectedBidID));
                SelectedBidID = (DataSourceBids[0] != null ? DataSourceBids[0].BidID : -1);
            }
            catch
            {
            }
        }
        DataSourceBids = Bid.GetBidByBidIsActive(false);
        NonActiveBidsList1.DataBind(DataSourceBids);
        DataSourceBids = Bid.GetBidByBidIsActive(true);
        ActiveBidsList1.DataBind(DataSourceBids);
        SetLayout(LayoutType.View);
    }
    protected void btnsave_Click(object sender, ImageClickEventArgs e)
    {
        if (BidsViewEdit1.EndEdit())
        {
            Bid obj = DataSourceBids.FindByBidID(SelectedBidID);

            Bid.Update(obj);
            //Set the feedback text here.....
            DataSourceBids = Bid.GetBidByBidIsActive(false);
            NonActiveBidsList1.DataBind(DataSourceBids);
            DataSourceBids = Bid.GetBidByBidIsActive(true);
            ActiveBidsList1.DataBind(DataSourceBids);
            Response.AddHeader("Refresh", "0");
            SetLayout(LayoutType.View);
        }
    }


    #endregion

    #endregion

    #region Private

    #region SetLayout

    /// <summary>
    /// Configures the UI for the specified 'uiLayoutType'.
    /// </summary>
    protected void SetLayout(LayoutType uiLayoutType)
    {
        switch (uiLayoutType)
        {
            case LayoutType.Add:
            case LayoutType.Edit:

                pnlNonActiveBids.Visible = false;
                pnlActiveBids.Visible = false;
                pnlBidsViewEdit.Visible = true;
                pnlButtons.Visible = true;
                break;

            case LayoutType.View:
            case LayoutType.Search:

                pnlNonActiveBids.Visible = true;
                pnlActiveBids.Visible = true;
                pnlBidsViewEdit.Visible = false;
                pnlButtons.Visible = false;
                break;

            default:
                throw new NotImplementedException("The specified LayoutType is not supported in this release.");
        }

        //SetInitialFocus(uiLayoutType);
    }

    #endregion

    #endregion
}
